<template>
  <el-dialog :visible.sync="isShow" title="用户编辑"
    width="400px"
    top="60px">
    <el-form ref="form" :model="fields" :rules="rules" label-width="80px">
      <el-form-item label="部门" prop="deptId">
        <el-cascader
          v-model="fields.deptId"
          size="small"
          style="width:100%"
          :options="parents"
          :props="{
            label: 'name',
            value: 'id',
            children: 'childrens',
            checkStrictly: true,
            emitPath: false
          }"
          clearable
          placeholder="请选择部门"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model.trim="fields.nickName"
          autocomplete="off"
          size="small"
          placeholder="请输入用户昵称"
          maxlength="64"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model.trim="fields.phone"
          autocomplete="off"
          size="small"
          placeholder="请输入手机号"
          maxlength="11"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model.trim="fields.email"
          autocomplete="off"
          size="small"
          placeholder="邮箱"
          maxlength="256"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="loading" size="mini" @click="closePage"
        >取消</el-button
      >
      <el-button
        :loading="loading"
        type="primary"
        size="mini"
        @click="onOkBtnClick"
        >保存</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { saveOrUpdate, get } from '@/api/user'
export default {
  props: {
    value: {
      type: Object,
      default: () => Object.create({})
    }
  },
  data() {
    return {
      isShow: true,
      fields: {},
      parents: [],
      rules: {
        deptId: { required: true, message: '请选中部门', trigger: 'blur' }
      }
    }
  },
  watch: {
    isShow: function() {
      this.$emit('input', null)
    }
  },
  created() {
    this.hasEnt = this.$parent.hasEnt
    this.get(this.value.id || null)
  },
  methods: {
    onOkBtnClick() {
      this.submitForm()
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        valid &&
          saveOrUpdate(this.fields)
            .then(() => {
              this.isShow = false
              this.$parent.onSearchBtnClick()
            })
            .catch(() => {})
      })
    },
    resetForm() {
      this.$refs['form'].resetFields()
    },
    async get(id) {
      const res = await get(id)
      if (!res.hasEnt) {
        res.hasEnt = 'N'
      }
      this.fields = res
      this.parents = res.resouces || []
      if (!this.fields.deptId && this.parents.length) {
        this.fields.deptId = this.parents[0].deptId
      }
    },
    closePage(){
      this.isShow = false
      this.resetForm()
    }
  }
}
</script>
